package me.zhengjie.timing.task;

import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.entity.*;
import me.zhengjie.sqlservice.*;
import me.zhengjie.util.state.TaskStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
public class TaskTiming {
    private static final Logger haikang = LoggerFactory.getLogger("haiKang");

    private final WcsInboundMissionService wcsInboundMissionService;

    private final WcsInboundMissionHistoryService wcsInboundMissionHistoryService;

    private final WcsOutboundMissionService wcsOutboundMissionService;

    private final WcsOutboundMissionHistoryService wcsOutboundMissionHistoryService;

    private final WcsDeliveryMissionService wcsDeliveryMissionService;

    private final WcsDeliveryMissionHistoryService wcsDeliveryMissionHistoryService;

    private final WcsInboundAbnormalMissionService wcsInboundAbnormalMissionService;

    private final WcsInboundAbnormalMissionHistoryService wcsInboundAbnormalMissionHistoryService;

    @Transactional
    @Scheduled(cron = "0 0 0 ? * ?")
    public void taskTiming(){
        //查询出入库表将已完成任务移到历史表
        List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectMissionStatus(TaskStatus.Task_complete.getStatus());
        List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectMissionStatus(TaskStatus.Task_complete.getStatus());
        List<WcsDeliveryMission> wcsDeliveryMissionList = wcsDeliveryMissionService.selectMissionStatus(TaskStatus.Task_complete.getStatus());
        List<WcsInboundAbnormalMission> wcsInboundAbnormalMissionList = wcsInboundAbnormalMissionService.selectMissionStatus(TaskStatus.Task_complete.getStatus());
        haikang.info("定时移除到历史表;{},出库：{},配送：{},入库异常：{},",wcsInboundMissionList,wcsOutboundMissionList,wcsDeliveryMissionList,wcsInboundAbnormalMissionList);
        //入库
        if (!wcsInboundMissionList.isEmpty()){
            //入库任务不为空，将数据移到历史表
            for (WcsInboundMission wcsInboundMission : wcsInboundMissionList) {
                //移到历史表
                WcsInboundMissionHistory wcsInboundMissionHistory = new WcsInboundMissionHistory();
                BeanUtils.copyProperties(wcsInboundMission, wcsInboundMissionHistory);
                wcsInboundMissionHistory.setId(null);
                boolean b3 = wcsInboundMissionHistoryService.insertInboundHistory(wcsInboundMissionHistory);
                if (b3){
                    haikang.error("海康上报入库放货，插入历史表失败");
                }
                //删除入库任务
                boolean b1 = wcsInboundMissionService.deleteInboundId(wcsInboundMission.getId());
            }
        }
        //出库
        if (!wcsOutboundMissionList.isEmpty()){
            //出库任务不为空，将数据移到历史表
            for (WcsOutboundMission wcsOutboundMission : wcsOutboundMissionList) {
                WcsOutboundMissionHistory wcsOutboundMissionHistory = new WcsOutboundMissionHistory();
                BeanUtils.copyProperties(wcsOutboundMission, wcsOutboundMissionHistory);
                wcsOutboundMissionHistory.setId(null);
                boolean b3 = wcsOutboundMissionHistoryService.insertOutboundHistory(wcsOutboundMissionHistory);
                boolean b1 = wcsOutboundMissionService.deleteOutboundId(wcsOutboundMission.getId());
                if (!b1){
                    haikang.error("出库放货完成，移到历史表失败");
                    throw new RuntimeException("出库放货完成，移到历史表失败");
                }
            }
        }
        //配送
        if (!wcsDeliveryMissionList.isEmpty()){
            for (WcsDeliveryMission wcsDeliveryMission : wcsDeliveryMissionList) {
                //移到历史表
                WcsDeliveryMissionHistory wcsDeliveryMissionHistory = new WcsDeliveryMissionHistory();
                BeanUtils.copyProperties(wcsDeliveryMission, wcsDeliveryMissionHistory);
                wcsDeliveryMissionHistory.setId(null);
                boolean b3 = wcsDeliveryMissionHistoryService.insertDeliveryHistory(wcsDeliveryMissionHistory);
                if (!b3){
                    haikang.error("海康上报入库放货，插入历史表失败");
                }
                boolean b1 = wcsDeliveryMissionService.deleteDeliveryId(wcsDeliveryMission.getId());
            }
        }
        //异常任务
        if (!wcsInboundAbnormalMissionList.isEmpty()){
            for (WcsInboundAbnormalMission wcsInboundAbnormalMission : wcsInboundAbnormalMissionList) {
                //移到历史表
                WcsInboundAbnormalMissionHistory wcsInboundAbnormalHistoryMission = new WcsInboundAbnormalMissionHistory();
                BeanUtils.copyProperties(wcsInboundAbnormalMission,wcsInboundAbnormalHistoryMission );
                wcsInboundAbnormalHistoryMission.setId(null);
                boolean b3 = wcsInboundAbnormalMissionHistoryService.insertAbnormalHistory(wcsInboundAbnormalHistoryMission);
                if (b3){
                    haikang.error("海康上报入库放货，插入历史表失败");
                }
                //删除入库任务
                boolean b1 = wcsDeliveryMissionService.deleteDeliveryId(wcsInboundAbnormalMission.getId());
            }
        }
    }
}
